//
//  SettimeModel.m
//  ebook
//
//  Created by ChenNan on 16/3/28.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "SettimeModel.h"

@implementation SettimeModel

//是否开启了时间段自动夜间模式
+ (BOOL)isAutoNight{
    NSDictionary *dic = kPeriodTimeOfNight;
    return [dic[@"isOn"] intValue]==1;
}

+ (BOOL)isInNightTimeInteval
{
    NSDictionary *dic = kPeriodTimeOfNight;
    NSString *time = [NSString stringWithFormat:@"%@-%@", dic[@"fromTime"], dic[@"toTime"]];
    if (!time) {//第一次没有时间
        time = [NSString stringWithFormat:@"19:00-05:00"];
    }
    
//    if ([dic[@"isOn"] intValue]==0)//未打开夜间时间段设置
//      return NO;

    //time
    NSArray *array = [time componentsSeparatedByString:@"-"];//例如：19:00-05:00
    NSArray *startTimeArray = [array[0] componentsSeparatedByString:@":"];
    NSArray *endTimeArray = [array[1] componentsSeparatedByString:@":"];
    
    //当前时间（24h）
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"];//hh-->12; HH-->24
    NSString *currentDateStr = [formatter stringFromDate:[NSDate date]];//16:20
    NSArray *currentTimeArray = [currentDateStr componentsSeparatedByString:@":"];
    
    int sH = [startTimeArray[0] intValue];//开始时间：小时
    int sM = [startTimeArray[1] intValue];//开始时间：分钟
    
    int eH = [endTimeArray[0] intValue];//结束时间：小时
    int eM = [endTimeArray[1] intValue];//结束时间：分钟
    
    int cH = [currentTimeArray[0] intValue];//当前时间：小时
    int cM = [currentTimeArray[1] intValue];//当前时间：分钟
    
    BOOL ret = NO;
    if ([dic[@"isOn"] isEqualToString:@"1"]) {//打开了夜间时间段设置
        if (sH*60+sM<eH*60+eM && sH*60+sM<cH*60+cM && cH*60+cM<eH*60+eM) {//同一天
            ret =  YES;
        }else if (sH*60+sM>eH*60+eM) {//跨天
            if(sH*60+sM<cH*60+cM || cH*60+cM<eH*60+eM)
                ret = YES;
        }
    }
    
    return ret;
}

@end
